from rest_framework import serializers

from xyz.models import User, Label, Question, Reply, Article, Enterprise, Recruit


class ArticleInfoSerializers(serializers.ModelSerializer):
    class Meta:
        model = Article
        fields = ['id', 'title']


class UserInfoserializers(serializers.ModelSerializer):
    articles = ArticleInfoSerializers(read_only=True, many=True)
    fans = serializers.PrimaryKeyRelatedField(read_only=True, many=True)

    class Meta:
        model = User
        fields = ['id', 'username', 'avatar', 'articles', 'fans']


class UserAnswerInfoSerializers(serializers.ModelSerializer):
    class Meta:
        model = User
        fields = ['id', 'username', 'avatar']


class SubsInfoSerializers(serializers.ModelSerializer):
    user = UserAnswerInfoSerializers(read_only=True, many=True)

    class Meta:
        model = Reply
        fields = ['id', 'content', 'createtime', 'useful_count', 'unuseful_count', 'user']


class LabelModelSerializer(serializers.ModelSerializer):
    class Meta:
        model = Label
        fields = ['id', 'label_name']


class QuestionModelSerializer(serializers.ModelSerializer):
    labels = serializers.StringRelatedField(read_only=True, many=True)

    class Meta:
        model = Question
        fields = ['id', 'createtime', 'labels', 'reply', 'replyname', 'replytime', 'title', 'unuseful_count',
                  'useful_count', 'user', 'visits']


class ReplyModelSerializer(serializers.ModelSerializer):
    # subs = SubsInfoSerializers(read_only=True)
    user = UserAnswerInfoSerializers(read_only=True)

    class Meta:
        model = Reply
        # fields = ['id', 'content', 'createtime', 'useful_count', 'problem', 'unuseful_count', 'user', 'parent']
        fields = '__all__'
        # , 'subs', 'user'


class ArticleModelSerializer(serializers.ModelSerializer):
    user = UserInfoserializers(read_only=True)

    class Meta:
        model = Article
        fields = ['id', 'title', 'content', 'createtime', 'user', 'collected_users', 'image']
    # , 'collected'


class EnterpriseModelSerializer(serializers.ModelSerializer):
    recruits = serializers.PrimaryKeyRelatedField(read_only=True, many=True)

    class Meta:
        model = Enterprise
        fields = ['id', 'name', 'labels', 'logo', 'recruits', 'summary']


class UserModelSerializer(serializers.ModelSerializer):
    password = serializers.CharField(write_only=True, required=False)

    labels = LabelModelSerializer(read_only=True, many=True)
    questions = QuestionModelSerializer(read_only=True, many=True)
    answer_question = ReplyModelSerializer(read_only=True, many=True)
    collected_articles = ArticleModelSerializer(read_only=True, many=True)
    enterpises = EnterpriseModelSerializer(read_only=True, many=True)

    class Meta:
        model = User
        fields = ['id', 'username', 'mobile', 'realname', 'birthday', 'sex', 'avatar', 'website', 'email', 'city',
                  'address', 'labels', 'questions', 'answer_question', 'collected_articles', 'enterpises', 'password']
        # depth = 1


# ==============================================================================
class UserSerializer(serializers.ModelSerializer):
    labels = LabelModelSerializer(read_only=True, many=True)
    questions = QuestionModelSerializer(read_only=True, many=True)
    answer_question = ReplyModelSerializer(read_only=True, many=True)
    collected_articles = ArticleModelSerializer(read_only=True, many=True)
    enterpises = EnterpriseModelSerializer(read_only=True, many=True)

    class Meta:
        model = User
        fields = ['realname', 'sex', 'birthday', 'website', 'mobile', 'email', 'city', 'address', 'avatar', 'labels',
                  'questions', 'answer_question', 'collected_articles', 'enterpises']
